<template>
  <view>
    <!-- 选择地址 -->
    <view class="buttonBox" v-if="JSON.stringify(address)==='{}'" >
        <button type="primary" @click="chooseAddress" size="mini">请选择收获地址+</button>
    </view>
    <!-- 地址区域 -->
    <view class="addressBox" v-else  @click="chooseAddress">
      <!-- 收货人与电话区域 -->
      <view class="numTop">
        <view class="row1-left">
            <view class="username">收货人：<text>{{address.userName}}</text></view>
        </view>
        <view class="row1-right">
           <view class="phone">电话：<text>{{address.telNumber}}</text></view>
           <uni-icons type="arrowright" size="16"></uni-icons>
        </view>
      </view>
      <!-- 收获地址区域 -->
      <view class="address">
        <view class="row2-left">收货地址:</view>
        <view class="row2-right">{{siteLocation}}</view>
      </view>
    </view>
    
    <image src="../../static/cart_border@2x.png" class="bottomImg"></image>
  </view>
</template>

<script>
  import {mapMutations,mapState,mapGetters} from "vuex"
  export default {
    name:"my-address",
    data() {
      return {
        // address:{}  //收货地址
      };
    },
    computed:{
      ...mapState('moduleUser',['address']),
      ...mapGetters('moduleUser',['siteLocation'])
    },
    methods:{
      ...mapMutations('moduleUser',['updateAddress']),
      // 获取收货人的地址
    async  chooseAddress(){
        const [err,succ] =await uni.chooseAddress().catch(err=>err)
        // 判断 正确的消息是不 chooseAddress:ok
        if(err==null && succ.errMsg ==='chooseAddress:ok' ){
          // 赋值
          // this.address = succ
          this.updateAddress(succ)
        }
        // 用户没有授权
        if(err &&( err.errMsg === "chooseAddress:fail auth deny" || err.errMsg === 'chooseAddress:fail authorize no response')){
          this.reAuth() // 调用 this.reAuth() 方法，向用户重新发起授权申请
        }
      },
      // 重新发起授权
      reAuth(){
        const [err2, confirmResult] = uni.showModal({
          title:"检测到您没打开地址权限，是否去设置打开？",
          confirmText:'确认',
          cancelText:'取消'
        })
      if(confirmResult.cancel) return uni.$showMsg('你取消了位置授权')
      if(confirmResult.confirm) return uni.openSetting({
          success:(settingResult)=>{
            if(settingResult.authSetting['scope.address']) return uni.$showMsg('授权成功,请选择地址')
            if(!settingResult.authSetting['scope.address']) return uni.$showMsg('你取消了地址授权')
          }
      })
      }
    }
  }
</script>

<style lang="scss">
.buttonBox {
 height: 90px;
 display: flex;
 justify-content: center;
 align-items: center;
}
.addressBox{
  margin-top: 10px;
   font-size: 12px;
    height: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 5px;
  .numTop{
    display: flex;
    justify-content: space-between;
    .row1-right {
      display: flex;
      .phone {
        margin-right: 5px;
      }
    }
  }
  .address{
    display: flex;
    align-items: center;
    margin-top: 10px;

    .row2-left{
      white-space: nowrap;
    }
  }
}
.bottomImg{
   display: block;
  width: 100%;
  height: 5px;
}
</style>
